Page({

  data: {
    showName: false,
    showGender: false,
    showTime:false,
    name: '',
    gender: '',
    birthday:'',
    tempName: '',
    tempGender: '',
    columns: ['男', '女'],
    showDate:'',
    tempDate: '',
    photo:''
  },

  onLoad() {
    this.getUserInfo()
  },

  async getUserInfo() {
    const res = await wx.http.get('/v1_0/user/profile')
    if(res.statusCode ===401) return wx.utils.toast('登录已过期')
    console.log('用户信息', res.data.data)
    const { photo, name, gender, birthday } = res.data.data
    this.setData({photo, name, gender, birthday})
  },

  chooseAvatar(e) {
    console.log(e.detailavatarUrl)
    this.setData({
      photo: e.detail.avatarUrl
    })
    // wx 不支持 patch 请求
    // wx.uploadFile({
    //   url: wx.http.baseURL + '/v1_0/user/photo',
    //   filePath: e.detail.avatarUrl,
    //   name: 'photo',
    //   header: {
    //     Authorization: getApp().token
    //   },
    //   success: (res) => {
    //     console.log(res)
    //   }
    // })
  },

  editName() {
    this.setData({
      tempName:this.data.name,
      showName:true
    })
  },
  cancleEditName() {
    this.setData({
      showName:false
    })
  },
 async confirmEditName() {
    this.setData({
      name: this.data.tempName,
      showName:false
    })
    //wx 不支持 patch 请求
    // const res = await wx.http.patch('/v1_0/user/profile')
    // console.log(res)
  },
  editGender() {
    this.setData({
      tempGender:this.data.gender,
      showGender:true
    })
  },
  cancleEditGender() {
    this.setData({
      showGender:false
    })
  },
  confirmEditGender() {
    this.setData({
      gender: this.data.tempGender,
      showGender:false
    })
  },
  onChange(event) {
    const { value } = event.detail;
    console.log(value)
    this.setData({
      tempGender: value === '男' ? 1 : 0
    })
  },

  editTime() {
    const temp = this.data.birthday.split('-').join(',')
    console.log(temp)
    this.setData({
      showDate: new Date(temp).getTime(),
      showTime:true
    })
  },
  

  cancleEditTime() {
    this.setData({
      showTime:false
    })
  },

  confirmEditTime() {
    this.setData({
      birthday: this.data.tempDate,
      showTime:false
    })
  },
  onInput({detail}) {
    const year = new Date(detail).getFullYear()
    const month = new Date(detail).getMonth() + 1
    const date = new Date(detail).getDate()
    this.setData({
      tempDate: [year, month, date].join('-')
    })
  }


})